package com.itheima.reggie.common;

import com.itheima.reggie.controller.EmployeeController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class GloBalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> sQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException ex){
        log.error(ex.getMessage());
        String name = ex.getMessage().split("'")[1];
        return R.error(name+"已重复");
    }

    @ExceptionHandler(CustomException.class)
    public R<String> customExceptionHandler(CustomException ex){
        log.info(ex.getMessage());
        return R.error(ex.getMessage());
    }

    @ExceptionHandler(OtherException.class)
    public R<String> otherException(OtherException ex){
        log.info(ex.getMessage());
        ex.printStackTrace();
        return R.error("网络繁忙,请稍后再试");
    }


}
